package controllers.commands
{
	import flash.events.TimerEvent;
	
	import mx.logging.ILogger;
	
	import controllers.commands.base.ICommand;
	import controllers.managers.ScheduleManager;
	import controllers.managers.SingletonManager;
	
	import model.context.DeviceMonitorContext;
	import model.context.base.IContext;
	import model.protocal.DeviceProtocal;
	import model.registry.EnvConst;
	import model.vo.DeviceVOList;
	
	import net.NetLoader;
	
	import utils.DialogUtil;
	import utils.LogUtil;
	
	/***
	 *  文件名: QueryDevicesCommand.as
	 *  简介: 进入多车监控界面后，发起查询，查询返回结果后将结果回显到界面上
	 *            关键点: 有个全局监控变量，如果当前已经在查询，则不予调用(可暂时不做)
	 *  作者:  kant
	 *  时间:  Dec 3, 2012 10:25:05 PM
	 *
	 **/
	public class QueryDevicesCommand implements ICommand
	{
		private var context:DeviceMonitorContext;
		public static var NAME : String = "QueryDevicesCommand.as";
		private var logger:ILogger =  LogUtil.getInst().getLogger( NAME );
		
		public function QueryDevicesCommand(context:IContext)
		{
			this.context = DeviceMonitorContext(context);
		}
		
		public function setContext(context:Object):void
		{
		}
		
		public function execute():void
		{			
			doSendProtocal();	
			var scheduleMgr:ScheduleManager =ScheduleManager( SingletonManager.getServ(ScheduleManager.NAME) );
			scheduleMgr.createTimer(NAME, EnvConst.MONITOR_INTERVAL, handlerMarker).start();
		}
		
		private function handlerMarker(evt:TimerEvent):void
		{
			logger.info("更新设备列表 。。。。");
			doSendProtocal();	
		}
		
		/**
		 *  发送
		 **/
		private function doSendProtocal():void
		{
			var prococal:DeviceProtocal = new DeviceProtocal(this.context);
			var sendUrl:String = prococal.getProtocal();
			//LM 0614 设置多设备监视的每页数量
			sendUrl += "&perpage=200";
			logger.info("发送协议: " + sendUrl);
			var netSender:NetLoader = new NetLoader();
			netSender.load(sendUrl, onRespList);			
		}
		
		/**
		 *  返回信息
		 **/
		private function onRespList(evt:Object):void
		{
			if ( evt.status == 1 )
			{
				//绘制结果
				setMultiMonitorDP( evt.data );
			}
			else
			{
				DialogUtil.openError("请检查选择的设备是否正常。", "错误");
				logger.error("返回协议错误:" + evt.status);
			}
		}
		
		/**
		 *  设置多机监控列表
		 **/
		private function setMultiMonitorDP(ds:Object):void
		{
			//将相关数据源带过去
			var dsList:Array = new DeviceVOList(ds).getDeviceVOList();
			//更新信息
			this.context.locInfoList = dsList;
			new UpdateSelectedDevicesCommand(this.context).execute();
			//在地图上绘制marker
			new CreateBatchMarkersCommand(this.context).execute();
		}
	}
}